import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from "rxjs/Observable";
import "rxjs/add/observable/of";
import {PageParamsModel} from "../../../model/common/page-params.model";

@Injectable()
export class ClientIndividualProvider {
    prePath: string;

    data = {
        pageNum: 1,
        totalItemCount: 690,
        pagedItems: [
            {
                "name": "蒋44580636",
                "clientId": "4301646077",
                "sex": "F",
                "mobilePhone": "13813565361",
                "vipFlag": null,
                "premium": "7535",
                "totalPolicy": "3",
                "age": "36"
            },
            {
                "name": "张89035943",
                "clientId": "4301658905",
                "sex": "F",
                "mobilePhone": "13813111113",
                "vipFlag": null,
                "premium": "7827",
                "totalPolicy": "2",
                "age": "34"
            },
            {
                "name": "张14469121",
                "clientId": "4300364902",
                "sex": "F",
                "mobilePhone": "13811535316",
                "vipFlag": null,
                "premium": "10250",
                "totalPolicy": "5",
                "age": "46"
            },
            {
                "name": "杨32240088",
                "clientId": "4300089562",
                "sex": "M",
                "mobilePhone": "13905351356",
                "vipFlag": null,
                "premium": "11642",
                "totalPolicy": "2",
                "age": "48"
            },
            {
                "name": "王34324772",
                "clientId": "4301809256",
                "sex": "F",
                "mobilePhone": "13631156156",
                "vipFlag": null,
                "premium": "6592",
                "totalPolicy": "2",
                "age": "29"
            },
            {
                "name": "王78745763",
                "clientId": "4301835213",
                "sex": "F",
                "mobilePhone": "13605338133",
                "vipFlag": null,
                "premium": "9189",
                "totalPolicy": "2",
                "age": "41"
            },
            {
                "name": "陆37760939",
                "clientId": "4301837740",
                "sex": "M",
                "mobilePhone": "13811316886",
                "vipFlag": null,
                "premium": "2383",
                "totalPolicy": "1",
                "age": "34"
            },
            {
                "name": "周29699906",
                "clientId": "4301418263",
                "sex": "M",
                "mobilePhone": "13805613116",
                "vipFlag": null,
                "premium": "4849",
                "totalPolicy": "1",
                "age": "42"
            },
            {
                "name": "朱53915261",
                "clientId": "4301321573",
                "sex": "F",
                "mobilePhone": null,
                "vipFlag": null,
                "premium": "22143",
                "totalPolicy": "3",
                "age": "48"
            },
            {
                "name": "柳16072839",
                "clientId": "4301433749",
                "sex": "M",
                "mobilePhone": "18913853653",
                "vipFlag": null,
                "premium": "3693",
                "totalPolicy": "1",
                "age": "50"
            },
            {
                "name": "徐78774754",
                "clientId": "4300375857",
                "sex": "F",
                "mobilePhone": null,
                "vipFlag": null,
                "premium": "2394",
                "totalPolicy": "1",
                "age": "50"
            },
            {
                "name": "孙61405061",
                "clientId": "4301522867",
                "sex": "F",
                "mobilePhone": null,
                "vipFlag": null,
                "premium": "4326",
                "totalPolicy": "1",
                "age": "38"
            },
            {
                "name": "乔32311085",
                "clientId": "4301562356",
                "sex": "M",
                "mobilePhone": "18968133886",
                "vipFlag": null,
                "premium": "3744",
                "totalPolicy": "1",
                "age": "36"
            },
            {
                "name": "秦78253862",
                "clientId": "4301574757",
                "sex": "M",
                "mobilePhone": "13813558616",
                "vipFlag": null,
                "premium": "5276",
                "totalPolicy": "1",
                "age": "37"
            },
            {
                "name": "陈68183090",
                "clientId": "4300219442",
                "sex": "F",
                "mobilePhone": "13605635365",
                "vipFlag": null,
                "premium": "3989",
                "totalPolicy": "2",
                "age": "33"
            },
            {
                "name": "朱49105665",
                "clientId": "4300238830",
                "sex": "F",
                "mobilePhone": "13905356555",
                "vipFlag": null,
                "premium": "28450",
                "totalPolicy": "3",
                "age": "37"
            },
            {
                "name": "陶28914436",
                "clientId": "4303503035",
                "sex": "F",
                "mobilePhone": "13625651818",
                "vipFlag": null,
                "premium": "7145",
                "totalPolicy": "1",
                "age": "38"
            },
            {
                "name": "张91110184",
                "clientId": "4300797798",
                "sex": "F",
                "mobilePhone": null,
                "vipFlag": null,
                "premium": "0",
                "totalPolicy": "1",
                "age": "57"
            },
            {
                "name": "张67219444",
                "clientId": "4300011799",
                "sex": "F",
                "mobilePhone": "17313318585",
                "vipFlag": null,
                "premium": "0",
                "totalPolicy": "3",
                "age": "52"
            },
            {
                "name": "徐91055566",
                "clientId": "4300830042",
                "sex": "M",
                "mobilePhone": "18005335568",
                "vipFlag": null,
                "premium": "2138",
                "totalPolicy": "1",
                "age": "61"
            }
        ]
    };

    constructor(public http: HttpClient) {
        this.prePath = 'party/v1';
    }

    /**
     * 获取个险客户列表
     * @returns {any}
     */
    getIndividualList(notLoading = 0): Observable<any> {
        const p = `client`;
        const url = `/${this.prePath}/${p}?not-loading=${notLoading}`;
        return this.http.get(url);
    }

    /**
     * 获取个险客户列表
     * @param {string} orderType
     * @param {number} notLoading
     * @param {PageParamsModel} pageModel
     * @returns {Observable<any>}
     */
    getIndividualPageList(notLoading = 0, pageModel: PageParamsModel): Observable<any> {
        const p = `client`;
        const url = `/${this.prePath}/${p}`;
        return this.http.get(url, {
            params: {
                pageNum: '' + pageModel.pageNum,
                pageSize: '' + pageModel.pageSize,
                orderType: pageModel.orderType ? pageModel.orderType : '',
                keyword: pageModel.condition.keyword ? pageModel.condition.keyword : ''
            }
        });
    }

    /**
     * 获取所有个险客户
     * @returns {Observable<any>}
     */
    getIndividualAll(keyWord?: string, notLoading=0): Observable<any> {
        const p = `clientAll`;
        const url = `/${this.prePath}/${p}`;
        return this.http.get(url, {
            params: {
                'not-loading':''+ notLoading,
                keyword: keyWord ? keyWord : ''
            }
        });
    }


    /**
     * 过滤为空的电话号码
     * @param clientList
     */
    checkMobileNull(clientList) {
        clientList.forEach(client => {
            const mobileList = [];
            if (client.mobilePhone && client.mobilePhone.length) {
                client.mobilePhone.forEach(mobile => {
                    if (mobile && mobile.mobile) {
                        mobileList.push({
                            id: mobile.id,
                            mobile: mobile.mobile
                        });
                    }
                });
            }
            client.mobilePhone = mobileList;
        });
        return clientList;
    }
}
